﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JEdwonTune.Lib;
using JEdwonTune.Lib.Hibernate;
namespace JEdwonTune.Hibernate {
    public partial class FrmDbView : FrmMDIChild {
        public FrmDbView() {
            InitializeComponent();
        }
        private Boolean _ConnectionOk = false;
        public Boolean ConnectionOk {
            set {
                _ConnectionOk = value;
                if (!_ConnectionOk) _Handler = null;
            }
            get {
                return _ConnectionOk;
            }
        }
        private DbHandler _Handler = null;
        public DbHandler Handler {
            set { _Handler = value; }
            get { return _Handler; }
        }

        private IList<ColumnEO> _ColumnsList;
        public IList<ColumnEO> ColumnsList {
            get { return _ColumnsList; }
        }

        private String _SelectedTable;
        private void FrmDbView_Load(object sender, EventArgs e) {
            this.gvTable.AutoGenerateColumns = false;
            this.gvColumns.AutoGenerateColumns = false;
            this.tsbtn_MysqlViewScript.Enabled = (_Handler.DbService == DbServerType.MySql6);

            try {
                this.gvTable.DataSource = _Handler.LoadTables();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
            this.gvTable.Focus();
        }

        private void gvTable_RowEnter(object sender, DataGridViewCellEventArgs e) {
            DataGridViewRow row = this.gvTable.Rows[e.RowIndex];
            _SelectedTable = row.Cells["GvColName"].Value.ToString();
            _ColumnsList = _Handler.LoadColumns(_SelectedTable);
            this.gvColumns.DataSource = _ColumnsList;
        }

        private void btnBuildMapping_Click(object sender, EventArgs e) {

        }

        private void tsbtnBuildMapping_Click(object sender, EventArgs e) {
            FrmMappingBuilder frm = new FrmMappingBuilder();
            frm.TableName = _SelectedTable;
            frm.ColumnList = _ColumnsList;
            frm.Pk = _Handler.LoadPKs(_SelectedTable);
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK) { 
                
            }
        }

        private void tsbtnRefresh_Click(object sender, EventArgs e) {
            try {
                this.gvTable.DataSource = _Handler.LoadTables();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }

        }

        private void tsbtnConnect_Click(object sender, EventArgs e) {
            FrmDbConenct frm = new FrmDbConenct();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK) {
                _Handler = frm.Handler;
            }
            try {
                this.gvTable.DataSource = _Handler.LoadTables();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbtn_MysqlViewScript_Click(object sender, EventArgs e) {
            FrmExportViewDef frm = new FrmExportViewDef();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK) {
                
            }
        }


        
    }
}
